package com.lcn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcn.base.BaseInfoProperties;
import com.lcn.enums.FriendRequestVerifyStatus;
import com.lcn.enums.YesOrNo;
import com.lcn.mapper.FriendRequestMapper;
import com.lcn.mapper.FriendshipMapper;
import com.lcn.pojo.FriendRequest;
import com.lcn.pojo.Friendship;
import com.lcn.pojo.bo.NewFriendRequestBO;
import com.lcn.pojo.vo.NewFriendsVO;
import com.lcn.service.FriendRequestService;
import com.lcn.utils.PagedGridResult;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class FriendRequestServiceImpl extends BaseInfoProperties implements FriendRequestService {

    @Resource
    private FriendRequestMapper friendRequestMapper;

    @Resource
    private FriendshipMapper friendshipMapper;

    /**
     * 发起一个好友请求
     *
     * @param newFriendRequestBO
     */
    @Override
    @Transactional
    public void addNewRequest(NewFriendRequestBO newFriendRequestBO) {
        String myId = newFriendRequestBO.getMyId();
        String friendId = newFriendRequestBO.getFriendId();
        // 删除原来的好友请求
        QueryWrapper<FriendRequest> deleteWrapper = new QueryWrapper<FriendRequest>()
                .eq("my_id", myId)
                .eq("friend_id", friendId);
        friendRequestMapper.delete(deleteWrapper);
        // 添加新请求
        FriendRequest friendRequest = new FriendRequest();
        BeanUtils.copyProperties(newFriendRequestBO, friendRequest);
        friendRequest.setVerifyStatus(FriendRequestVerifyStatus.WAIT.type);
        friendRequest.setRequestTime(LocalDateTime.now());
        friendRequestMapper.insert(friendRequest);
    }

    /**
     * 分页查询历史好友请求
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PagedGridResult queryNewFriendList(String userId, Integer page, Integer pageSize) {
        Page<NewFriendsVO> newFriendsVOPage = new Page<>(page, pageSize);
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("myId", userId);
        friendRequestMapper.queryNewFriendList(newFriendsVOPage, paramsMap);
        return setterPagedGridPlus(newFriendsVOPage);
    }

    /**
     * 通过好友请求
     *
     * @param friendRequestId
     * @param friendRemark
     */
    @Override
    @Transactional
    public void pass(String friendRequestId, String friendRemark) {
        FriendRequest friendRequest = queryById(friendRequestId);
        String mySelfId = friendRequest.getFriendId(); // 自己的id
        String friendId = friendRequest.getMyId(); // 对方的id

        LocalDateTime now = LocalDateTime.now();

        Friendship friendshipSelf = new Friendship();
        friendshipSelf.setMyId(mySelfId);
        friendshipSelf.setFriendId(friendId);
        friendshipSelf.setFriendRemark(friendRemark);
        friendshipSelf.setIsMsgIgnore(YesOrNo.NO.type);
        friendshipSelf.setIsBlack(YesOrNo.NO.type);
        friendshipSelf.setCreatedTime(now);
        friendshipSelf.setUpdatedTime(now);

        // 对面的备注在请求中，角色互换
        Friendship friendshipOpposite = new Friendship();
        friendshipOpposite.setMyId(friendId);
        friendshipOpposite.setFriendId(mySelfId);
        friendshipOpposite.setFriendRemark(friendRequest.getFriendRemark());
        friendshipOpposite.setIsMsgIgnore(YesOrNo.NO.type);
        friendshipOpposite.setIsBlack(YesOrNo.NO.type);
        friendshipOpposite.setCreatedTime(now);
        friendshipOpposite.setUpdatedTime(now);

        friendshipMapper.insert(friendshipSelf);
        friendshipMapper.insert(friendshipOpposite);

        // 更新双方的请求状态为[已通过]
        friendRequest.setVerifyStatus(FriendRequestVerifyStatus.SUCCESS.type);
        friendRequestMapper.updateById(friendRequest);

        // A向B发起申请，过期了，B又向A申请
        QueryWrapper<FriendRequest> updateWrapper = new QueryWrapper<FriendRequest>()
                .eq("my_id", mySelfId)
                .eq("friend_id", friendId);
        FriendRequest oppositeFriendRequest = new FriendRequest();
        oppositeFriendRequest.setVerifyStatus(FriendRequestVerifyStatus.SUCCESS.type);
        friendRequestMapper.update(oppositeFriendRequest, updateWrapper);

    }

    /**
     * 根据id查询好友关系表
     *
     * @param friendRequestId
     * @return
     */
    @Override
    public FriendRequest queryById(String friendRequestId) {
        return friendRequestMapper.selectById(friendRequestId);
    }

}
